package com.yfeng.common.utils;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yfeng.common.dao.IDao;

@Component
public class PageUtil {

	private static final Log log = LogFactory.getLog(PageUtil.class);
	@Autowired
	public IDao baseDao;
	 // private int pagesize = 3; //一页显示6条数据

    public int pagesize=15; //一页显示条数
    
    
    public int getPagesize() {
		return pagesize;
	}
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}
	public int getPageSize()
    {
        return pagesize;
    }
    /// <summary>
    /// 取记录数
    /// </summary>
    /// <param name="strSql"></param>
    /// <returns></returns>
    public int getRecordCount(String strSql) throws Exception 
    {
        int recordCount = 0;      
        try
        {          
        	
            return  baseDao.getRowCount(strSql);          

        }
        catch (Exception e)
        {
            throw new Exception(e.getMessage(),e);
        }
       

    }

     /// <summary>
     /// 取共有多少页
     /// </summary>
     /// <param name="recordCount"></param>
     /// <returns></returns>
     public int getPageCount(int recordCount)
     {
         int f = recordCount/pagesize;
         if (recordCount % pagesize > 0) f++;
         f = f == 0 ? 1 : f;
       //  decimal d = Convert.ToDecimal(f);
       //  int pageCount = Convert.ToInt32(Math.Round(d));
         int pageCount = (int) Math.floor(f);
         return pageCount;
     }

 
     
     /**
      * 分页查询
      * 当有多个表时候，调用该查询方法
      * @param currentPage 当前第几页
      * @param select select 语句，要查询的列
      * @param table 表名,多个表用逗号分隔
      * @param conds 条件语句 and xx=xx 不要加where
      * @param px 排序语句 order by xx
      * @param tablealias 表别名如果是table t 那么只需要传入t
      * @return
     * @throws Exception 
      */
     public List query(int currentPage,String select,String table, String conds,String px) throws Exception
     {
        
    	 String strSql = "select "+select+" from "+table+" where 1=1 "+conds+" "+px+" limit "+pagesize *(currentPage-1)+","+pagesize;
//         if (currentPage == 1)
//         {
//        	 strSql="select "+select+" from "+table+" where 1=1 "+conds+" "+px+" limit "+pagesize *(currentPage-1)+","+pagesize;
//             strSql = "select t.* from (select top " + pagesize + "  "+select+"  from " + table + " where 1=1 " + conds + " "+px+") t";
//
//         }
//         else if (currentPage == 2)
//         {
//             strSql = "select top " + pagesize + "  "+select+"  from " + table + " where 1=1 " + conds + " and  "+tablealias+".ID not in(select top " + pagesize + " "+tablealias+".ID from " + table + " where 1=1 " + conds + " "+px+") "+px;
//             
//         }
//         else
//         {
//             strSql = "select top " + pagesize + "  "+select+"  from " + table + " where 1=1 " + conds + " and  "+tablealias+".ID not in(select top " + (pagesize * (currentPage - 1)) + " "+tablealias+".ID from " + table + " where 1=1 " + conds + " "+px+") " + px ;
//         }
       
         //string strSql = "select top " + pagesize + " * from " + table + " where 1=1 " + conds + " and id not in (select top " + ((2 - 1) * 5) + " id from " + table + " where 1=1 " + conds + ") ";
         try {
			return baseDao.query(strSql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage(),e);
		}
	
     }
     

     public String printHtml(int currentPage,int recordCount,int pageCount)
     {
    	 String html = "";
         int t = getPageCount(recordCount);
         String s = "共"+recordCount+"条记录 当前"+currentPage+"/"+pageCount+"页 ";
         String head = "<a href='?page=1'>首页</a>&nbsp;";
         String foot = "&nbsp;<a href='?page=" + t + "'>尾页</a>";
         if (currentPage< pageCount && currentPage==1)
         {
             int n = currentPage + 1;
             html = "<a href='?page="+n+"'>下页</a> ";  
         }
         else if (currentPage < pageCount && currentPage != 1)
         {
             int p = currentPage - 1;
             int n = currentPage + 1;
             html = "<a href='?page="+p+"'>上页</a> <a href='?page="+n+"'>下页</a>";
         }
         else if (currentPage == pageCount && pageCount>1)
         {
             int p = currentPage - 1;
             html = "<a href='?page=" + p + "'>上页</a> ";
         }
         String jump="  前往第<input name='page' id='page' size=10  onblur='gopage()'/>页";
         String script ="<script> " +
  		"function gopage(){" +
  		"var p=$('#page').val();" +
 		"var result = p.match(/^[0-9]+$/g);"+
 		"if(p==''){return false;}else if(result == null){alert('非法数字');$('#page').val('');}else{window.location=\"?page=\"+p+\" \";}" +
 		"}" +
  		"</script>"; 
         return s+head+html+foot+jump+script;
         
     }

    
   

     public String printHtml(int currentPage, int recordCount, int pageCount,String cond)
     {
    	 String html = "";
         int t = getPageCount(recordCount);
         String s = "共" + recordCount + "条记录 当前" + currentPage + "/" + pageCount + "页 ";
         String head = "<a href='?page=1"+cond+"'>首页</a>&nbsp;";
         String foot = "&nbsp;<a href='?page=" + t + cond+"'>尾页</a>";
         if (currentPage < pageCount && currentPage == 1)
         {
             int n = currentPage + 1;
             html = "<a href='?page=" + n + cond+"'>下页</a> ";
         }
         else if (currentPage < pageCount && currentPage != 1)
         {
             int p = currentPage - 1;
             int n = currentPage + 1;
             html = "<a href='?page=" + p + cond + "'>上页</a> <a href='?page=" + n + cond + "'>下页</a>";
         }
         else if (currentPage == pageCount && pageCount > 1)
         {
             int p = currentPage - 1;
             html = "<a href='?page=" + p + cond+"'>上页</a> ";
         }
         String jump="  前往第<input name='page' id='page' size=10 onblur='gopage();'/>页";
         String script ="<script> " +
         		"function gopage(){" +
         		"var p=$('#page').val();" +
         		"var result = p.match(/^[0-9]+$/g);"+
         		"if(p==''){return false;}else if(result == null){alert('非法数字');$('#page').val('');}else{window.location=\"?page=\"+p+\""+cond+" \";}" +
         		"}" +
         		"</script>";
         return s + head + html + foot + jump+script;

     }

    

}
